<?php
class ModelDesignLayout extends Model {
	public function getLayout($route) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "layout_route WHERE '" . $this->db->escape($route) . "' LIKE route AND store_id = '" . (int)$this->config->get('config_store_id') . "' ORDER BY route DESC LIMIT 1");

		if ($query->num_rows) {
			return $query->row['layout_id'];
		} else {
			return 0;
		}
	}
	
	public function getLayoutModules($layout_id, $position) {
	    $sql = "SELECT * FROM " . DB_PREFIX . "layout_module WHERE layout_id = '" . (int)$layout_id . "' AND position = '" . $this->db->escape($position) . "' ORDER BY sort_order";
	    //print_r($sql);//exit;
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "layout_module WHERE layout_id = '" . (int)$layout_id . "' AND position = '" . $this->db->escape($position) . "' ORDER BY sort_order");
		
		return $query->rows;
	}
	
	//后来添加的
	public function getLayouts($data = array()) {
	 $sql = "SELECT * FROM " . DB_PREFIX . "layout";
	
	 $sort_data = array('name');
	
	 if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
	  $sql .= " ORDER BY " . $data['sort'];
	 } else {
	  $sql .= " ORDER BY name";
	 }
	
	 if (isset($data['order']) && ($data['order'] == 'DESC')) {
	  $sql .= " DESC";
	 } else {
	  $sql .= " ASC";
	 }
	
	 if (isset($data['start']) || isset($data['limit'])) {
	  if ($data['start'] < 0) {
	   $data['start'] = 0;
	  }
	
	  if ($data['limit'] < 1) {
	   $data['limit'] = 20;
	  }
	
	  $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
	 }
	
	 $query = $this->db->query($sql);
	
	 return $query->rows;
	}
}